import { infoApi, loginApi, logoutApi } from "@/api/user";
import { getToken, removeToken, setToken } from "@/utils/cookie";
import { defineStore } from "pinia";
export const useUserStore = defineStore('user', {
    state: () => ({
        token: getToken() || '',
        userInfo:{}
    }),
    actions: {
        setToken(token) {
            this.token = token
            setToken(token)
        },
        setUserInfo(userInfo) {
            this.userInfo = userInfo
        },
        async loginAction(data) {
            const resp = await loginApi(data)
            const token = resp.data
            this.setToken(token)
        },
        logoutLocalAction() {
            this.token = ''
            removeToken()
        },
        async logoutAction() {
            await logoutApi()
            this.logoutLocalAction()
        },
        async getUserInfo() {
            const resp = await infoApi()
            this.setUserInfo(resp.data)
        }
    }
})